import React from 'react';
import {Spin, Card, Button, Row, Col, Table, Form, Popconfirm, Message as msg, Modal} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import cloneDeep from 'lodash/cloneDeep';
import {FORM_OPTIONS, TABLE, DATE_FORMAT, FIXED_COLUMN_WIDTH, FORM_ROW_OPTIONS, DATETIME_FORMAT} from '../constants';
import {addApplyReport, getDetail, updateApplyReport, getSaleOrderForecastOrders} from './api';
import {formatDateTime, formatPercent, conventEnumValueToString} from '../utils';
import {uuid, addMonths} from './utils';
import {salePlanAssembleMethods} from './constants';
import ProductSelectPanel from './ProductSelectPanel';
import {salePlanAssembleMethod, isYesNo, salePlanOrderResultJudgeResult, currency} from '../Enum';
import TextInput from 'Shared/components/TextInput';
import SearchInput from 'Shared/components/SearchInput';
import ProductCategorySelectPanel from './ProductCategorySelectPanel';
import {injectIntl, language} from './intl';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import WrappedSelect from '../common/WrappedSelect';
import WrappedInputNumber from '../common/WrappedInputNumber';
import ImportPanel from './ImportPanel';
import styles from './style.css';
import Return from '../common/Return';
import {newOrUpdateOrderValidator} from './validator';
import {unionBy} from 'lodash';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
const {Description} = DescriptionList;
const FormItem = Form.Item;
const today = new Date();

const salePlanAssembleMethodToList = salePlanAssembleMethod.toList(language);
const salePlanAssembleMethodsToList = salePlanAssembleMethods.toList(language);
const currencyToList = currency.toList(language);
const isYesNoToList = isYesNo.toList(language);
class AddPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        isValidate: false,
        show1ProductSelectPanel: false,
        show2ProductSelectPanel: false,
        showImportPanel: false,
        showProductCategorySelectPanel: false,
        data: {},
        details: [],
        approveDetails: [],
        detailId: '',
    }
    componentDidMount() {
        const id = this.props.id;
        if(id) {
            this.setState({loading: true});
            getDetail(id)
                .then(res => {
                    if(res.ok)
                        this.setState({
                            loading: false,
                            data: res.data,
                            details: res.data.details.map(v => (
                                Object.assign({}, v, {
                                    newModel: v.newModel === true ? 1 : 0,
                                })
                            )),
                            approveDetails: res.data.approveDetails,
                        });
                    else
                        this.setState({loading: false});
                });
        }
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    onChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onClickOkOnPopconfirm = () => {
        this.props.onCancel();
    }
    onSubmit = () => {
        const data = this.state.data;
        const details = this.state.details;
        const isValid = newOrUpdateOrderValidator(data, details);
        if(!isValid) {
            this.setState({
                isValidate: true
            });
            return;
        }
        const condition = {
            dealerId: data.dealerId,
            brandId: data.brandId,
            remark: data.remark,
            rowVersion: data.rowVersion,
            details: details.map(v => ({
                assembleMethod: v.assembleMethod,
                materialGroupKDBOMCode: v.materialGroupKDBOMCode,
                referenceMaterialGroupKDBOMCode: v.referenceMaterialGroupKDBOMCode,
                versionCode: v.versionCode,
                modelId: v.modelId,
                newModel: v.newModel === 1,
                n1Quantity: v.n1Quantity,
                n2Quantity: v.n2Quantity,
                n3Quantity: v.n3Quantity,
                n4Quantity: v.n4Quantity,
                n5Quantity: v.n5Quantity,
                n6Quantity: v.n6Quantity,
                detailRemark: v.detailRemark,
                n7Quantity: v.n7Quantity,
                n8Quantity: v.n8Quantity,
                n9Quantity: v.n9Quantity,
                n10Quantity: v.n10Quantity,
                n11Quantity: v.n11Quantity,
                n12Quantity: v.n12Quantity,
                engineCylinder: v.engineCylinder,
                engineModelName: v.engineModelName,
                gearboxModel: v.gearboxModel,
                transmission: v.transmission,
                driveMode: v.driveMode,
                version: v.version,
                unitPrice: v.unitPrice,
                currency: v.currency,
            })),
        };
        this.setState({loading: true});
        if(this.props.id)
            updateApplyReport(this.props.id, condition).then(res => {
                if(res.ok)
                    this.props.successReturn();
                if(!this.isunmount)
                    this.setState({loading: false});
            });
        else
            addApplyReport(condition).then(res => {
                if(res.ok)
                    this.props.successReturn();
                if(!this.isunmount)
                    this.setState({loading: false});
            });
    }
    onClickSearchProductCategoryBtn = id => {
        this.setState({
            detailId: id,
            showProductCategorySelectPanel: true
        });
    }
    onCloseProductCategorySelectPanel = () => {
        this.setState({showProductCategorySelectPanel: false});
    }
    onSelectProductCategory = records => {
        const details = this.state.details || [];
        const id = this.state.detailId;
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    versionId: records.id,
                    versionCode: records.code,
                    modelCode: records.modelCode,
                    manufacturerName: records.manufacturerName
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true,
            showProductCategorySelectPanel: false
        });
    }
    on1ClickAddDetailBtn = () => {
        if(!this.state.data.brandId || !this.state.data.dealerId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'addPanel.message.brandIdandDealerId',
                defaultMessage: '请先选择品牌和总代'
            }));
        this.setState({
            show1ProductSelectPanel: !this.state.show1ProductSelectPanel
        });
    }
    on2ClickAddDetailBtn = () => {
        if(!this.state.data.brandId || !this.state.data.dealerId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'addPanel.message.brandIdandDealerId',
                defaultMessage: '请先选择品牌和总代'
            }));
        this.setState({
            show2ProductSelectPanel: !this.state.show2ProductSelectPanel
        });
    }
    onSelectProduct1 = value => {
        const {details, data} = this.state;
        value.map(p => {
            const detail = this.state.details.find(d => d.id === p.id);
            if(!detail) {
                const detail = {
                    id: p.id,
                    materialGroupKDBOMCode: p.materialGroupCode,
                    modelCode: p.modelCode,
                    assembleMethod: salePlanAssembleMethod.CBU,
                    versionCode: p.versionCode,
                    manufacturerName: p.manufacturerName,
                    versionId: p.versionId,
                    transmission: p.transmission,
                    engineModelName: p.engineModelName,
                    gearboxModel: p.gearboxModel
                };
                details.push(detail);
            }
            return p;
        });
        this.props.getQuantity({
            brandId: data.brandId,
            dealerId: data.dealerId,
            details: details.map(v => ({
                assembleMethod: v.assembleMethod,
                materialGroupKDBOMCode: v.materialGroupKDBOMCode,
                referenceMaterialGroupKDBOMCode: v.referenceMaterialGroupKDBOMCode,
            }))
        }).then(res => {
            let tmp = [];
            if(res.ok) {
                const data = res.data.content;
                // eslint-disable-next-line array-callback-return
                tmp = details.map(v => {
                    const quantity = data.find(o => o.assembleMethod === v.assembleMethod &&
                        o.materialGroupKDBOMCode === v.materialGroupKDBOMCode);
                    if(quantity)
                        return Object.assign({}, v, {
                            n1QuantityYearBefore: quantity.n1QuantityYearBefore,
                            n1QuantityMonthBefore: quantity.n1QuantityMonthBefore
                        });
                    return v;
                });
            }
            this.setState({
                show1ProductSelectPanel: false,
                hasOperat: true,
                details: tmp,
            });
        });
        this.setState({
            show1ProductSelectPanel: false
        });
    }
    onSelectProduct2 = value => {
        const {details, data} = this.state;
        value.map(p => {
            const detail = this.state.details.find(d => d.id === p.id);
            if(!detail) {
                const detail = {
                    id: p.id,
                    referenceMaterialGroupKDBOMCode: p.materialGroupCode,
                    modelCode: p.modelCode,
                    assembleMethod: salePlanAssembleMethod.CBU,
                    versionCode: p.versionCode,
                    manufacturerName: p.manufacturerName,
                    versionId: p.versionId,
                    transmission: p.transmission,
                    engineModelName: p.engineModelName,
                    gearboxModel: p.gearboxModel
                };
                details.push(detail);
            }
            return p;
        });
        this.props.getQuantity({
            brandId: data.brandId,
            dealerId: data.dealerId,
            details: details.map(v => ({
                assembleMethod: v.assembleMethod,
                materialGroupKDBOMCode: v.materialGroupKDBOMCode,
                referenceMaterialGroupKDBOMCode: v.referenceMaterialGroupKDBOMCode,
            }))
        }).then(res => {
            let tmp = [];
            if(res.ok) {
                const data = res.data.content;
                // eslint-disable-next-line array-callback-return
                tmp = details.map(v => {
                    const quantity = data.find(o => o.assembleMethod === v.assembleMethod &&
                        o.referenceMaterialGroupKDBOMCode === v.referenceMaterialGroupKDBOMCode);
                    if(quantity)
                        return Object.assign({}, v, {
                            n1QuantityYearBefore: quantity.n1QuantityYearBefore,
                            n1QuantityMonthBefore: quantity.n1QuantityMonthBefore
                        });
                    return v;
                });
            }
            this.setState({
                show2ProductSelectPanel: false,
                hasOperat: true,
                details: tmp
            });
        });
        this.setState({
            show2ProductSelectPanel: false
        });
    }
    onDeleteAll = () => {
        this.setState({
            details: [],
            hasOperat: true
        });
    }
    onDeleteDetail = e => {
        const id = e.target.dataset.id;
        const details = this.state.details.filter(p => p.id !== id);
        this.setState({
            details,
            hasOperat: true
        });
    }
    onChangeAll = (value, id, e) => {
        const details = this.state.details || [];
        const name = e.target.name;
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    [name]: value
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    onChangeQuantity = (value, name, e) => {
        const details = this.state.details || [];
        const id = e.target.id;
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    [name]: value
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    onChangeKDBOM = (value, id, e) => {
        const {details, data} = this.state || [];
        const name = e.target.name;
        const detail = details.find(v => v.id === id);
        if(detail.assembleMethod)
            this.props.getQuantity({
                brandId: data.brandId,
                dealerId: data.dealerId,
                details: {
                    assembleMethod: detail.assembleMethod,
                    materialGroupKDBOMCode: name === 'materialGroupKDBOMCode' ? value : detail.materialGroupKDBOMCode,
                    referenceMaterialGroupKDBOMCode: name === 'referenceMaterialGroupKDBOMCode' ? value : detail.referenceMaterialGroupKDBOMCode,
                }
            }).then(res => {
                if(res.ok) {
                    const data = res.data.content;
                    // eslint-disable-next-line array-callback-return
                    const quantity = data.find(o => o.assembleMethod === detail.assembleMethod &&
                        o.materialGroupKDBOMCode === (name === 'materialGroupKDBOMCode' ? value : detail.materialGroupKDBOMCode) &&
                        o.referenceMaterialGroupKDBOMCode === (name === 'referenceMaterialGroupKDBOMCode'
                            ? value : detail.referenceMaterialGroupKDBOMCode));
                    if(quantity) {
                        const tmp = details.map(d => {
                            if(d.id === id)
                                return Object.assign({}, d, {
                                    [name]: value,
                                    n1QuantityYearBefore: quantity.n1QuantityYearBefore,
                                    n1QuantityMonthBefore: quantity.n1QuantityMonthBefore
                                });
                            return d;
                        });
                        this.setState({
                            details: tmp,
                            hasOperat: true
                        });
                    } else {
                        const tmp = details.map(d => {
                            if(d.id === id)
                                return Object.assign({}, d, {
                                    [name]: value
                                });
                            return d;
                        });
                        this.setState({
                            details: tmp,
                            hasOperat: true
                        });
                    }
                } else {
                    const tmp = details.map(d => {
                        if(d.id === id)
                            return Object.assign({}, d, {
                                [name]: value
                            });
                        return d;
                    });
                    this.setState({
                        details: tmp,
                        hasOperat: true
                    });
                }
            });
        else {
            const tmp = details.map(d => {
                if(d.id === id)
                    return Object.assign({}, d, {
                        [name]: value
                    });
                return d;
            });
            this.setState({
                details: tmp,
                hasOperat: true
            });
        }
    }
    onUpdateassembleMethod = (assembleMethod, record) => {
        const {details, data} = this.state || [];
        const detail = details.find(v => v.id === record.id);
        if(detail.materialGroupKDBOMCode || detail.referenceMaterialGroupKDBOMCode)
            this.props.getQuantity({
                brandId: data.brandId,
                dealerId: data.dealerId,
                details: {
                    assembleMethod,
                    materialGroupKDBOMCode: detail.materialGroupKDBOMCode,
                    referenceMaterialGroupKDBOMCode: detail.referenceMaterialGroupKDBOMCode,
                }
            }).then(res => {
                if(res.ok) {
                    const data = res.data.content;
                    // eslint-disable-next-line array-callback-return
                    const quantity = data.find(o => o.assembleMethod === assembleMethod &&
                        o.materialGroupKDBOMCode === detail.materialGroupKDBOMCode &&
                        o.referenceMaterialGroupKDBOMCode === detail.referenceMaterialGroupKDBOMCode);
                    if(quantity) {
                        const tmp = details.map(d => {
                            if(d.id === record.id)
                                return Object.assign({}, d, {
                                    assembleMethod,
                                    n1QuantityYearBefore: quantity.n1QuantityYearBefore,
                                    n1QuantityMonthBefore: quantity.n1QuantityMonthBefore
                                });
                            return d;
                        });
                        this.setState({
                            details: tmp,
                            hasOperat: true,
                            loading: false
                        });
                    } else {
                        const tmp = details.map(d => {
                            if(d.id === record.id)
                                return Object.assign({}, d, {
                                    assembleMethod,
                                });
                            return d;
                        });
                        this.setState({
                            details: tmp,
                            hasOperat: true,
                            loading: false
                        });
                    }
                } else {
                    const tmp = details.map(d => {
                        if(d.id === record.id)
                            return Object.assign({}, d, {
                                assembleMethod,
                            });
                        return d;
                    });
                    this.setState({
                        details: tmp,
                        hasOperat: true,
                        loading: false
                    });
                }
            });
        else {
            const tmp = details.map(d => {
                if(d.id === record.id)
                    return Object.assign({}, d, {
                        assembleMethod,
                    });
                return d;
            });
            if(!record.newModel && data.brandId && data.dealerId && record.assembleMethod && record.materialGroupKDBOMCode)
                this.onGetSaleOrderForecastOrders(assembleMethod, 'assembleMethod', record);
            else
                this.setState({
                    details: tmp,
                    hasOperat: true
                });
        }
    }
    onGetSaleOrderForecastOrders = (value, name, record) => {
        const data = this.state.data;
        const details = this.state.details || [];
        this.setState({loading: true});
        getSaleOrderForecastOrders({brandId: data.brandId,
            dealerId: data.dealerId,
            assembleMethod: record.assembleMethod,
            materialGroupKDBOMCode: record.materialGroupKDBOMCode})
            .then(res => {
                if(res.ok) {
                    const tmp = details.map(d => {
                        if(d.id === record.id)
                            return Object.assign({}, d, {
                                [name]: value,
                                n2ForecastOrderQuantity: res.data.content[0] ? res.data.content[0].n2ForecastOrderQuantity : 0,
                                n3ForecastOrderQuantity: res.data.content[0] ? res.data.content[0].n3ForecastOrderQuantity : 0,
                                n4ForecastOrderQuantity: res.data.content[0] ? res.data.content[0].n4ForecastOrderQuantity : 0,
                                n5ForecastOrderQuantity: res.data.content[0] ? res.data.content[0].n5ForecastOrderQuantity : 0,
                                n6ForecastOrderQuantity: res.data.content[0] ? res.data.content[0].n6ForecastOrderQuantity : 0,
                            });
                        return d;
                    });
                    this.setState({
                        loading: false,
                        details: tmp
                    });
                } else {
                    const tmp = details.map(d => {
                        if(d.id === record.id)
                            return Object.assign({}, d, {
                                [name]: value
                            });
                        return d;
                    });
                    this.setState({
                        details: tmp,
                        loading: false
                    });
                }
            });
    }
    onUpdatenewModel = (newModel, record) => {
        const data = this.state.data;
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === record.id)
                return Object.assign({}, d, {
                    newModel,
                });
            return d;
        });
        if(!newModel && data.brandId && data.dealerId && record.assembleMethod && record.materialGroupKDBOMCode)
            this.onGetSaleOrderForecastOrders(newModel, 'newModel', record);
        else
            this.setState({
                details: tmp,
                hasOperat: true
            });
    }
    onChangeMaterialGroupKDBOMCode = (materialGroupKDBOMCode, record) => {
        const data = this.state.data;
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === record.id)
                return Object.assign({}, d, {
                    materialGroupKDBOMCode,
                });
            return d;
        });
        if(!record.newModel && data.brandId && data.dealerId && record.assembleMethod && record.materialGroupKDBOMCode)
            this.onGetSaleOrderForecastOrders(materialGroupKDBOMCode, 'materialGroupKDBOMCode', record);
        else
            this.setState({
                details: tmp,
                hasOperat: true
            });
    }
    onUpdatenewCurrency = (currency, id) => {
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    currency,
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    
    onSelectDealer = (condition = {}) => {
        if(this.state.data.dealerId !== undefined && this.state.data.dealerId !== condition.id)
            Modal.confirm({
                title: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch2',
                    defaultMessage: '提示'
                }),
                content: this.props.intl.formatMessage({
                    id: 'newPanel.message.switchDealerId',
                    defaultMessage: '变更总代，将会清空清单数据，是否清空？'
                }),
                onOk: () => this.onChangeSwitchDealer(condition)
            });
        else
            this.setState(state => {
                const newData = cloneDeep(state.data);
                return {
                    hasOperat: true,
                    data: {
                        ...newData,
                        dealerId: condition.id,
                        dealerCode: condition.code,
                        dealerName: condition.name,
                        country: condition.countryName,
                        marketDepartmentName: condition.marketDepartmentName,
                    }
                };
            });
    };
    onChangeSwitchDealer = condition => {
        this.setState(state => {
            const newData = cloneDeep(state.data);
            return {
                hasOperat: true,
                data: {
                    brandId: newData.brandId,
                    dealerId: condition.id,
                    dealerCode: condition.code,
                    dealerName: condition.name,
                    country: condition.countryName,
                    marketDepartmentName: condition.marketDepartmentName,
                },
                details: []
            };
        });
    }
    onClickImportBtn = () => {
        if(!this.state.data.brandId || !this.state.data.dealerId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'updatePanel.message.brandIdandDealerId',
                defaultMessage: '请先选择品牌和总代'
            }));
        this.setState({showImportPanel: true});
    }
    
    onImportChange = records => {
        if(records.payload) {
            const {details, data} = this.state;
            const retailDetails = details;
            records.payload.content.forEach(r => {
                const index = retailDetails.findIndex(v => v.id === r.id);
                if(index === -1)
                    retailDetails.push({
                        id: r.id,
                        assembleMethod: r.assembleMethod,
                        materialGroupKDBOMCode: r.materialGroupKDBOMCode,
                        referenceMaterialGroupKDBOMCode: r.referenceMaterialGroupKDBOMCode,
                        versionCode: r.versionCode,
                        modelCode: r.modelCode,
                        newModel: r.newModel === true ? 1 : 0,
                        n1Quantity: r.n1Quantity,
                        n2Quantity: r.n2Quantity,
                        n3Quantity: r.n3Quantity,
                        n4Quantity: r.n4Quantity,
                        n5Quantity: r.n5Quantity,
                        n6Quantity: r.n6Quantity,
                        n7Quantity: r.n7Quantity,
                        n8Quantity: r.n8Quantity,
                        n9Quantity: r.n9Quantity,
                        n10Quantity: r.n10Quantity,
                        n11Quantity: r.n11Quantity,
                        n12Quantity: r.n12Quantity,
                        n2ForecastOrderQuantity: r.n2ForecastOrderQuantity,
                        n3ForecastOrderQuantity: r.n3ForecastOrderQuantity,
                        n4ForecastOrderQuantity: r.n4ForecastOrderQuantity,
                        n5ForecastOrderQuantity: r.n5ForecastOrderQuantity,
                        n6ForecastOrderQuantity: r.n6ForecastOrderQuantity,
                        detailRemark: r.detailRemark,
                        manufacturerName: r.manufacturerName,
                        versionId: r.versionId,
                        transmission: r.transmission,
                        engineModelName: r.engineModelName,
                        gearboxModel: r.gearboxModel,
                        engineCylinder: r.engineCylinder,
                        driveMode: r.driveMode,
                        version: r.version,
                        unitPrice: r.unitPrice,
                        currency: r.currency,
                    });
                else
                    retailDetails.splice(index, 1, {
                        id: r.id,
                        assembleMethod: r.assembleMethod,
                        materialGroupKDBOMCode: r.materialGroupKDBOMCode,
                        referenceMaterialGroupKDBOMCode: r.referenceMaterialGroupKDBOMCode,
                        versionCode: r.versionCode,
                        modelCode: r.modelCode,
                        newModel: r.newModel === true ? 1 : 0,
                        n1Quantity: r.n1Quantity,
                        n2Quantity: r.n2Quantity,
                        n3Quantity: r.n3Quantity,
                        n4Quantity: r.n4Quantity,
                        n5Quantity: r.n5Quantity,
                        n6Quantity: r.n6Quantity,
                        n2ForecastOrderQuantity: r.n2ForecastOrderQuantity,
                        n3ForecastOrderQuantity: r.n3ForecastOrderQuantity,
                        n4ForecastOrderQuantity: r.n4ForecastOrderQuantity,
                        n5ForecastOrderQuantity: r.n5ForecastOrderQuantity,
                        n6ForecastOrderQuantity: r.n6ForecastOrderQuantity,
                        detailRemark: r.detailRemark,
                        manufacturerName: r.manufacturerName,
                        versionId: r.versionId,
                        transmission: r.transmission,
                        engineModelName: r.engineModelName,
                        gearboxModel: r.gearboxModel,
                        engineCylinder: r.engineCylinder,
                        driveMode: r.driveMode,
                        version: r.version,
                        unitPrice: r.unitPrice,
                        currency: r.currency,
                    });
            });
            this.props.getQuantity({
                brandId: data.brandId,
                dealerId: data.dealerId,
                details: details.map(v => ({
                    assembleMethod: v.assembleMethod,
                    materialGroupKDBOMCode: v.materialGroupKDBOMCode,
                    referenceMaterialGroupKDBOMCode: v.referenceMaterialGroupKDBOMCode,
                }))
            }).then(res => {
                if(res.ok) {
                    const data = res.data.content;
                    // eslint-disable-next-line array-callback-return
                    retailDetails.map(v => {
                        const quantity = data.find(o => o.assembleMethod === v.assembleMethod &&
                            o.materialGroupKDBOMCode === v.materialGroupKDBOMCode &&
                            o.referenceMaterialGroupKDBOMCode === v.referenceMaterialGroupKDBOMCode);
                        if(quantity)
                            return Object.assign({}, v, {
                                n1QuantityYearBefore: quantity.n1QuantityYearBefore,
                                n1QuantityMonthBefore: quantity.n1QuantityMonthBefore
                            });
                        return v;
                    });
                }
                this.setState({
                    details: [...retailDetails],
                    hasOperat: true,
                    showImportPanel: false,
                    loading: false
                });
            });
        }
    }
    
    onCloseImportPanel = () => {
        this.setState({showImportPanel: false});
    }
    onClickAddDetailBtn = () => {
        if(!this.state.data.brandId || !this.state.data.dealerId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'updatePanel.message.brandIdandDealerId',
                defaultMessage: '请先选择品牌和总代'
            }));
        const details = this.state.details;
        this.setState({
            details: [
                ...details,
                {
                    id: uuid(),
                    materialGroupKDBOMCode: '',
                    referenceMaterialGroupKDBOMCode: '',
                    versionCode: '',
                    modelCode: ''
                }
            ]
        });
    };
    onChangeBrand = (value, name) => {
        if(this.state.data.brandId !== undefined && this.state.data.brandId !== value)
            Modal.confirm({
                title: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch2',
                    defaultMessage: '提示'
                }),
                content: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch',
                    defaultMessage: '变更品牌，将会清空清单数据，是否清空？'
                }),
                onOk: () => this.onChangeSwitch(value, name)
            });
        else
            this.setState({
                data: Object.assign({}, this.state.data, {
                    [name]: value
                }),
                hasOperat: true
            });
    }
    onChangeSwitch = (value, name) => {
        this.setState(state => {
            const newData = cloneDeep(state.data);
            return {
                hasOperat: true,
                data: {
                    dealerId: newData.dealerId,
                    dealerCode: newData.dealerCode,
                    dealerName: newData.dealerName,
                    country: newData.countryName,
                    marketDepartmentName: newData.marketDepartmentName,
                    [name]: value,
                },
                details: []
            };
        });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {data, details, isValidate, loading, approveDetails} = this.state;
        const {brands} = this.props.initEnumData;
        if(brands.length === 1 && !this.props.id) {
            data.brandId = brands[0].value;
            data.brandName = brands[0].text;
        }
        const dealerInfo = data.dealerId ? {
            id: data.dealerId,
            code: data.dealerCode,
            name: data.dealerName
        } : null;
        let total = 0;
        total = details.length;
        let detailTotal = 0;
        let n1Quantity = 0;
        let n1ModelCount = 0;
        let n1VersionCount = 0;
        let n1MaterialGroupCount = 0;
        let totalN1Quantity = 0;
        let totalN2Quantity = 0;
        let totalN3Quantity = 0;
        let totalN4Quantity = 0;
        let totalN5Quantity = 0;
        let totalN6Quantity = 0;
        let totalN7Quantity = 0;
        let totalN8Quantity = 0;
        let totalN9Quantity = 0;
        let totalN10Quantity = 0;
        let totalN11Quantity = 0;
        let totalN12Quantity = 0;

        let totalVarietyN1 = 0; totalVarietyN1 = details.filter(v => v.n1Quantity).length;
        let totalVarietyN2 = 0; totalVarietyN2 = details.filter(v => v.n2Quantity).length;
        let totalVarietyN3 = 0; totalVarietyN3 = details.filter(v => v.n3Quantity).length;
        let totalVarietyN4 = 0; totalVarietyN4 = details.filter(v => v.n4Quantity).length;
        let totalVarietyN5 = 0; totalVarietyN5 = details.filter(v => v.n5Quantity).length;
        let totalVarietyN6 = 0; totalVarietyN6 = details.filter(v => v.n6Quantity).length;
        let totalVarietyN7 = 0; totalVarietyN7 = details.filter(v => v.n7Quantity).length;
        let totalVarietyN8 = 0; totalVarietyN8 = details.filter(v => v.n8Quantity).length;
        let totalVarietyN9 = 0; totalVarietyN9 = details.filter(v => v.n9Quantity).length;
        let totalVarietyN10 = 0; totalVarietyN10 = details.filter(v => v.n10Quantity).length;
        let totalVarietyN11 = 0; totalVarietyN11 = details.filter(v => v.n11Quantity).length;
        let totalVarietyN12 = 0; totalVarietyN12 = details.filter(v => v.n12Quantity).length;
        if(details)
            details.forEach(d => {
                detailTotal += (d.n1Quantity || 0) + (d.n2Quantity || 0) + (d.n3Quantity || 0) +
                    (d.n4Quantity || 0) + (d.n5Quantity || 0) + (d.n6Quantity || 0) +
                    (d.n7Quantity || 0) + (d.n8Quantity || 0) + (d.n9Quantity || 0) +
                    (d.n10Quantity || 0) + (d.n11Quantity || 0) + (d.n12Quantity || 0);
                n1Quantity += (d.n1Quantity || 0);
                totalN1Quantity += (d.n1Quantity || 0);
                totalN2Quantity += (d.n2Quantity || 0);
                totalN3Quantity += (d.n3Quantity || 0);
                totalN4Quantity += (d.n4Quantity || 0);
                totalN5Quantity += (d.n5Quantity || 0);
                totalN6Quantity += (d.n6Quantity || 0);
                totalN7Quantity += (d.n7Quantity || 0);
                totalN8Quantity += (d.n8Quantity || 0);
                totalN9Quantity += (d.n9Quantity || 0);
                totalN10Quantity += (d.n10Quantity || 0);
                totalN11Quantity += (d.n11Quantity || 0);
                totalN12Quantity += (d.n12Quantity || 0);
            });
        const newDetails = details.map(v => {
            if(v.n1Quantity)
                return Object.assign({}, v, {
                    n1YearOnYearRate: v.n1QuantityYearBefore ? (v.n1Quantity - v.n1QuantityYearBefore) / v.n1QuantityYearBefore : 1,
                    n1MonthOnMonthRate: v.n1QuantityYearBefore ? (v.n1Quantity - v.n1QuantityMonthBefore) / v.n1QuantityMonthBefore : 1,
                });
            return v;
        });
        const groupByModelCode = unionBy(details.filter(v => !''.includes(v.modelCode)), 'modelCode');
        const groupByPublicationNumber = unionBy(details.filter(v => !''.includes(v.versionCode)), 'versionCode');
        const groupByKdBom = unionBy(details.filter(v => !''.includes(v.materialGroupKDBOMCode)), 'materialGroupKDBOMCode');
        const groupByReferenceKdBom = unionBy(details.filter(v => !''.includes(v.referenceMaterialGroupKDBOMCode) &&
            ''.includes(v.materialGroupKDBOMCode)), 'referenceMaterialGroupKDBOMCode');
        n1ModelCount = groupByModelCode.length;
        n1VersionCount = groupByPublicationNumber.length;
        n1MaterialGroupCount = groupByKdBom.length + groupByReferenceKdBom.length;
        const columns = [{
            title: <span className="ant-form-item-required">{formatMessage({
                id: 'addPanel.column.assembleMethod',
                defaultMessage: '装配方式'
            })}</span>,
            dataIndex: 'assembleMethod',
            render: (text, record) => {
                if(text === salePlanAssembleMethod.CBU) {
                    const validate = !text && isValidate ? {validateStatus: 'error'} : null;
                    return (
                        <FormItem {...validate}>
                            <WrappedSelect
                                allowClear
                                id={record.id}
                                disabled={true}
                                options={salePlanAssembleMethodToList}
                                value={text}
                                onChange={value => this.onUpdateassembleMethod(value, record)}/>
                        </FormItem>
                    );
                }
                const validate = !text && isValidate ? {validateStatus: 'error'} : null;
                return (
                    <FormItem {...validate}>
                        <WrappedSelect
                            allowClear
                            id={record.id}
                            options={salePlanAssembleMethodsToList}
                            value={text}
                            onChange={value => this.onUpdateassembleMethod(value, record)}/>
                    </FormItem>
                );
            }
        }, {
            title: formatMessage({
                id: 'addPanel.column.materialGroupKDBOMCode',
                defaultMessage: '物料组号或KDBOM编号'
            }),
            dataIndex: 'materialGroupKDBOMCode',
            render: (text, record) => {
                if(record.assembleMethod === salePlanAssembleMethod.CBU) {
                    const validate = (!record.materialGroupKDBOMCode && !record.referenceMaterialGroupKDBOMCode) &&
                    isValidate ? {validateStatus: 'error'} : null;
                    return (
                        <FormItem {...validate}>
                            <TextInput
                                id={record.id}
                                disabled={true}
                                name="materialGroupKDBOMCode"
                                value={text}
                                onBlur={value => this.onChangeMaterialGroupKDBOMCode(value, record)}/>
                        </FormItem>
                    );
                }
                const validate = (!record.materialGroupKDBOMCode && !record.referenceMaterialGroupKDBOMCode) &&
                isValidate ? {validateStatus: 'error'} : null;
                return (
                    <FormItem {...validate}>
                        <TextInput
                            id={record.id}
                            name="materialGroupKDBOMCode"
                            value={text}
                            onBlur={value => this.onChangeMaterialGroupKDBOMCode(value, record)}/>
                    </FormItem>
                );
            }
        }, {
            title: formatMessage({
                id: 'addPanel.column.referenceMaterialGroupKDBOMCode',
                defaultMessage: '参考物料组号或KDBOM编号'
            }),
            dataIndex: 'referenceMaterialGroupKDBOMCode',
            render: (text, record) => {
                if(record.assembleMethod === salePlanAssembleMethod.CBU) {
                    const validate = (!record.materialGroupKDBOMCode && !record.referenceMaterialGroupKDBOMCode) &&
                    isValidate ? {validateStatus: 'error'} : null;
                    return (
                        <FormItem {...validate}>
                            <TextInput
                                id={record.id}
                                disabled={true}
                                name="referenceMaterialGroupKDBOMCode"
                                value={text}
                                onBlur={this.onChangeAll} />
                        </FormItem>
                    );
                }
                const validate = (!record.materialGroupKDBOMCode && !record.referenceMaterialGroupKDBOMCode) &&
                    isValidate ? {validateStatus: 'error'} : null;
                return (
                    <FormItem {...validate}>
                        <TextInput
                            id={record.id}
                            name="referenceMaterialGroupKDBOMCode"
                            value={text}
                            onBlur={this.onChangeAll} />
                    </FormItem>
                );
            }
        }, {
            title: formatMessage({
                id: 'addPanel.column.modelCode',
                defaultMessage: '车型编号'
            }),
            dataIndex: 'modelCode'
        }, {
            title: formatMessage({
                id: 'addPanel.column.versionCode',
                defaultMessage: '版型编号'
            }),
            dataIndex: 'versionCode',
            render: (text, record) => {
                if(record.assembleMethod === salePlanAssembleMethod.CBU) {
                    const validate = !text && isValidate ? {validateStatus: 'error'} : null;
                    return (
                        <FormItem {...validate} required>
                            <TextInput
                                id={record.id}
                                disabled={true}
                                name="versionCode"
                                value={text}
                                onBlur={this.onChangeAll} />
                        </FormItem>
                    );
                }
                const validate = !text && isValidate ? {validateStatus: 'error'} : null;
                return (
                    <FormItem {...validate} required>
                        <SearchInput
                            id={record.id}
                            name="versionCode"
                            value={text}
                            onClickSearchBtn={() => this.onClickSearchProductCategoryBtn(record.id)}/>
                    </FormItem>
                );
            }
        }, {
            title: formatMessage({
                id: 'addPanel.column.manufacturerName',
                defaultMessage: '生产工厂'
            }),
            dataIndex: 'manufacturerName'
        }, {
            title: <span className="ant-form-item-required">{formatMessage({
                id: 'addPanel.column.newModel',
                defaultMessage: '是否新车型'
            })}</span>,
            dataIndex: 'newModel',
            render: (text, record) => {
                const validate = (text !== 1 && text !== 0) && isValidate ? {validateStatus: 'error'} : null;
                return (
                    <FormItem {...validate}>
                        <WrappedSelect
                            allowClear
                            id={record.id}
                            options={isYesNoToList}
                            value={text}
                            onChange={value => this.onUpdatenewModel(value, record)} />
                    </FormItem>
                );
            }
        }, {
            title: formatMessage({
                id: 'addPanel.column.engineCylinder',
                defaultMessage: '排量'
            }),
            dataIndex: 'engineCylinder',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="engineCylinder"
                        value={text}
                        onBlur={this.onChangeAll} />
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'addPanel.column.engineModelName',
                defaultMessage: '发动机型号'
            }),
            dataIndex: 'engineModelName',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="engineModelName"
                        value={text}
                        onBlur={this.onChangeAll} />
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'addPanel.column.gearboxModel',
                defaultMessage: '变速箱型号'
            }),
            dataIndex: 'gearboxModel',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="gearboxModel"
                        value={text}
                        onBlur={this.onChangeAll} />
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'addPanel.column.transmission',
                defaultMessage: '手自动'
            }),
            dataIndex: 'transmission',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="transmission"
                        value={text}
                        onBlur={this.onChangeAll} />
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'addPanel.column.driveMode',
                defaultMessage: '驱动方式'
            }),
            dataIndex: 'driveMode',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="driveMode"
                        value={text}
                        onBlur={this.onChangeAll} />
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'addPanel.column.version',
                defaultMessage: '版型'
            }),
            dataIndex: 'version',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="version"
                        value={text}
                        onBlur={this.onChangeAll} />
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'addPanel.column.unitPrice',
                defaultMessage: '单价'
            }),
            dataIndex: 'unitPrice',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="unitPrice"
                        value={text}
                        onBlur={this.onChangeQuantity}/>
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'addPanel.column.currency',
                defaultMessage: '币种'
            }),
            dataIndex: 'currency',
            width: 100,
            render: (text, record) => (
                <FormItem>
                    <WrappedSelect
                        allowClear
                        id={record.id}
                        options={currencyToList}
                        value={text}
                        onChange={this.onUpdatenewCurrency}/>
                </FormItem>
            )
        }, {
            title: addMonths(2) + formatMessage({
                id: 'addPanel.column.n1YearOnYearRate',
                defaultMessage: '月同比'
            }),
            dataIndex: 'n1YearOnYearRate',
            render: text => formatPercent(text, 1)
        }, {
            title: addMonths(2) + formatMessage({
                id: 'addPanel.column.n1MonthOnMonthRate',
                defaultMessage: '月环比'
            }),
            dataIndex: 'n1MonthOnMonthRate',
            render: text => formatPercent(text, 1)
        }, {
            title: addMonths(2) + formatMessage({
                id: 'addPanel.column.n1Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n1Quantity',
            render: (text, record) => (
                <FormItem >
                    <WrappedInputNumber
                        id={record.id}
                        name="n1Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity}/>
                </FormItem>
            )
        }, {
            title: addMonths(3) + formatMessage({
                id: 'addPanel.column.n2Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n2Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n2Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity}/>
                </FormItem>
            )
        }, {
            title: addMonths(4) + formatMessage({
                id: 'addPanel.column.n3Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n3Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n3Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity}/>
                </FormItem>
            )
        }, {
            title: addMonths(5) + formatMessage({
                id: 'addPanel.column.n4Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n4Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n4Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity}/>
                </FormItem>
            )
        }, {
            title: addMonths(6) + formatMessage({
                id: 'addPanel.column.n5Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n5Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n5Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity} />
                </FormItem>
            )
        }, {
            title: addMonths(7) + formatMessage({
                id: 'addPanel.column.n6Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n6Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n6Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity} />
                </FormItem>
            )
        }, {
            title: addMonths(8) + formatMessage({
                id: 'addPanel.column.n7Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n7Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n7Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity} />
                </FormItem>
            )
        }, {
            title: addMonths(9) + formatMessage({
                id: 'addPanel.column.n8Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n8Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n8Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity} />
                </FormItem>
            )
        }, {
            title: addMonths(10) + formatMessage({
                id: 'addPanel.column.n9Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n9Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n9Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity} />
                </FormItem>
            )
        }, {
            title: addMonths(11) + formatMessage({
                id: 'addPanel.column.n10Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n10Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n10Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity} />
                </FormItem>
            )
        }, {
            title: addMonths(12) + formatMessage({
                id: 'addPanel.column.n11Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n11Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n11Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity} />
                </FormItem>
            )
        }, {
            title: addMonths(13) + formatMessage({
                id: 'addPanel.column.n12Quantity',
                defaultMessage: '月订购数量'
            }),
            dataIndex: 'n12Quantity',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        name="n12Quantity"
                        value={text}
                        onBlur={this.onChangeQuantity} />
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'addPanel.column.detailRemark',
                defaultMessage: '备注'
            }),
            dataIndex: 'detailRemark',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="detailRemark"
                        value={text}
                        onBlur={this.onChangeAll} />
                </FormItem>
            )
        }, {
            title: details.length > 0
                ? (
                    <Popconfirm placement="topRight" title={formatMessage({
                        id: 'addPanel.validation.isDelete',
                        defaultMessage: '是否确认全部删除？'
                    })} onConfirm={this.onDeleteAll} >
                        <a>{formatMessage({
                            id: 'addPanel.validation.clear',
                            defaultMessage: '清空'
                        })}</a>
                    </Popconfirm>
                )
                : null,
            dataIndex: 'id',
            fixed: 'right',
            width: FIXED_COLUMN_WIDTH,
            render: text => <a data-id={text} onClick={this.onDeleteDetail}>{formatMessage({
                id: 'addPanel.validation.delete',
                defaultMessage: '删除'
            })}</a>
        }];
        const approveColumns = [{
            title: formatMessage({
                id: 'addPanel.column.approveSequence',
                defaultMessage: '审核序号'
            }),
            dataIndex: 'approveSequence',
        }, {
            title: formatMessage({
                id: 'addPanel.column.approverName',
                defaultMessage: '审核人姓名'
            }),
            dataIndex: 'approverName',
        }, {
            title: formatMessage({
                id: 'addPanel.column.approveTime',
                defaultMessage: '审核时间'
            }),
            dataIndex: 'approveTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'addPanel.column.approvedContent',
                defaultMessage: '审核意见'
            }),
            dataIndex: 'approvedContent',
        }, {
            title: formatMessage({
                id: 'addPanel.column.approveResult',
                defaultMessage: '判定结果'
            }),
            dataIndex: 'approveResult',
            render: text => conventEnumValueToString(salePlanOrderResultJudgeResult, text)
        }, {
            title: formatMessage({
                id: 'addPanel.column.nextOperation',
                defaultMessage: '下一步操作'
            }),
            dataIndex: 'nextOperation',
        }];
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Card title={formatMessage({
                        id: 'addPanel.column.common',
                        defaultMessage: '滚动计划信息'
                    })}>
                        <Form>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.planCode',
                                        defaultMessage: '计划编号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.id ? data.planCode : <span className="value-font">{formatMessage({
                                            id: 'addPanel.value.defalut',
                                            defaultMessage: '尚未生成'
                                        })}</span>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={this.props.intl.formatMessage({
                                        id: 'addPanel.form.dealerId',
                                        defaultMessage: '总代'
                                    })}{...FORM_OPTIONS.item} validateStatus={isValidate && !data.dealerId ? 'error' : null} required>
                                        {data.id ? `(${data.dealerCode})${data.dealerName}` : <DealerSelectPanel
                                            value={dealerInfo}
                                            onChange={this.onChange}
                                            onSelect={this.onSelectDealer} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.country',
                                        defaultMessage: '国家'
                                    })} {...FORM_OPTIONS.item}>
                                        {data.country}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.marketDepartmentName',
                                        defaultMessage: '大区'
                                    })} {...FORM_OPTIONS.item}>
                                        {data.marketDepartmentName}
                                    </FormItem>
                                </Col>
                                {brands.length !== 1 && <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.brandName',
                                        defaultMessage: '品牌'
                                    })}{...FORM_OPTIONS.item} validateStatus={isValidate && !data.brandId ? 'error' : null} required>
                                        {data.id ? `(${data.brandCode})${data.brandName}` : <WrappedSelect
                                            name="brandId"
                                            value={data.brandId}
                                            options={brands}
                                            onChange={this.onChangeBrand}/>}
                                    </FormItem>
                                </Col>}
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.createTime',
                                        defaultMessage: '提报日期'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatDateTime(today, DATE_FORMAT)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.submitYear',
                                        defaultMessage: '年份'
                                    })} {...FORM_OPTIONS.item}>
                                        {today.getFullYear()}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.submitMonth',
                                        defaultMessage: '月份'
                                    })} {...FORM_OPTIONS.item}>
                                        {today.getMonth() + 1}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={addMonths(2) + formatMessage({
                                        id: 'addPanel.operation.n1Quantity',
                                        defaultMessage: '月总数量'
                                    })} {...FORM_OPTIONS.item}>
                                        {n1Quantity}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={addMonths(2) + formatMessage({
                                        id: 'addPanel.operation.n1ModelCount',
                                        defaultMessage: '月车型种类数'
                                    })} {...FORM_OPTIONS.item}>
                                        {n1ModelCount}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={addMonths(2) + formatMessage({
                                        id: 'addPanel.operation.n1VersionCount',
                                        defaultMessage: '月版型种类数'
                                    })} {...FORM_OPTIONS.item}>
                                        {n1VersionCount}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={addMonths(2) + formatMessage({
                                        id: 'addPanel.operation.n1MaterialGroupCount',
                                        defaultMessage: '月物料组种类数'
                                    })} {...FORM_OPTIONS.item}>
                                        {n1MaterialGroupCount}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.total',
                                        defaultMessage: '总数量'
                                    })} {...FORM_OPTIONS.item}>
                                        {detailTotal}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.detailTotal',
                                        defaultMessage: '总品种数'
                                    })} {...FORM_OPTIONS.item}>
                                        {total}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row className="row-margin">
                                <Col>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.remark',
                                        defaultMessage: '备注'
                                    })} {...FORM_ROW_OPTIONS.item}>
                                        <TextInput
                                            name="remark"
                                            type="textarea"
                                            value={data.remark}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </Card>
                    <Card
                        title={formatMessage({
                            id: 'addPanel.operation.detail',
                            defaultMessage: '滚动计划清单'
                        })} extra={<div>
                            <Button
                                size="small"
                                onClick={this.onClickImportBtn}>
                                {formatMessage({
                                    id: 'addPanel.operation.import',
                                    defaultMessage: '导入清单'
                                })}
                            </Button>
                            <Button
                                size="small"
                                onClick={this.onClickAddDetailBtn}>
                                {formatMessage({
                                    id: 'addPanel.btn.addKDBOM',
                                    defaultMessage: '增加KDBOM清单'
                                })}
                            </Button>
                            <Button
                                size="small"
                                onClick={this.on1ClickAddDetailBtn}>
                                {formatMessage({
                                    id: 'addPanel.btn.add',
                                    defaultMessage: '增加物料组清单'
                                })}
                            </Button>
                            <Button
                                size="small"
                                onClick={this.on2ClickAddDetailBtn}>
                                {formatMessage({
                                    id: 'addPanel.btn.addReference',
                                    defaultMessage: '增加参考物料组清单'
                                })}
                            </Button>
                        </div>}>
                        <Table
                            className="white-space-nowrap"
                            columns={columns}
                            rowKey="id"
                            pagination={false}
                            dataSource={newDetails}
                            footer={() => <div>
                                <Row className={styles.alertMargin}>
                                    <DescriptionList>
                                        <Description term={addMonths(2) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN1Quantity}
                                        </Description>
                                        <Description term={addMonths(3) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN2Quantity}
                                        </Description>
                                        <Description term={addMonths(4) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN3Quantity}
                                        </Description>
                                        <Description term={addMonths(5) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN4Quantity}
                                        </Description>
                                        <Description term={addMonths(6) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN5Quantity}
                                        </Description>
                                        <Description term={addMonths(7) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN6Quantity}
                                        </Description>
                                        <Description term={addMonths(8) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN7Quantity}
                                        </Description>
                                        <Description term={addMonths(9) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN8Quantity}
                                        </Description>
                                        <Description term={addMonths(10) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN9Quantity}
                                        </Description>
                                        <Description term={addMonths(2) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN10Quantity}
                                        </Description>
                                        <Description term={addMonths(11) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN11Quantity}
                                        </Description>
                                        <Description term={addMonths(12) + formatMessage({
                                            id: 'newPanel.operation.totalQuantity',
                                            defaultMessage: '月份数量'
                                        })}>{totalN12Quantity}
                                        </Description>
                                    </DescriptionList>
                                </Row>
                                <Row className={styles.alertMargin}>
                                    <DescriptionList>
                                        <Description term={addMonths(2) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN1}
                                        </Description>
                                        <Description term={addMonths(3) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN2}
                                        </Description>
                                        <Description term={addMonths(4) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN3}
                                        </Description>
                                        <Description term={addMonths(5) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN4}
                                        </Description>
                                        <Description term={addMonths(6) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN5}
                                        </Description>
                                        <Description term={addMonths(7) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN6}
                                        </Description>
                                        <Description term={addMonths(8) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN7}
                                        </Description>
                                        <Description term={addMonths(9) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN8}
                                        </Description>
                                        <Description term={addMonths(10) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN9}
                                        </Description>
                                        <Description term={addMonths(11) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN10}
                                        </Description>
                                        <Description term={addMonths(12) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN11}
                                        </Description>
                                        <Description term={addMonths(13) + formatMessage({
                                            id: 'newPanel.operation.totalVariety',
                                            defaultMessage: '月份品种数'
                                        })}>{totalVarietyN12}
                                        </Description>
                                    </DescriptionList>
                                </Row>
                            </div>}
                            {...TABLE} />
                    </Card>
                    {this.props.id && <Card title={formatMessage({
                        id: 'addPanel.table.approveDetails',
                        defaultMessage: '审核清单'
                    })}>
                        <Table
                            className="white-space-nowrap"
                            columns={approveColumns}
                            rowKey="id"
                            pagination={false}
                            dataSource={approveDetails}
                            {...TABLE} />
                    </Card>}
                    <Card className={styles.optionBtn}>
                        <Button
                            type="primary"
                            onClick={this.onSubmit}
                            loading={loading}>
                            {formatMessage({
                                id: 'addPanel.operation.onSubmit',
                                defaultMessage: '保存'
                            })}
                        </Button>
                    </Card>
                </Spin>
                {
                    this.state.show1ProductSelectPanel && <ProductSelectPanel
                        brandId={data.brandId}
                        dealerId={data.dealerId}
                        selectKey={details.id}
                        onCancel={this.on1ClickAddDetailBtn}
                        onSelect={this.onSelectProduct1} />
                }
                {
                    this.state.show2ProductSelectPanel && <ProductSelectPanel
                        brandId={data.brandId}
                        dealerId={data.dealerId}
                        selectKey={details.id}
                        onCancel={this.on2ClickAddDetailBtn}
                        onSelect={this.onSelectProduct2} />
                }
                {
                    this.state.showImportPanel &&
                    <ImportPanel
                        brandId={data.brandId}
                        dealerId={data.dealerId}
                        selectKey={details.id}
                        onCancel={this.onCloseImportPanel}
                        onImportChange={this.onImportChange} />
                }
                {
                    this.state.showProductCategorySelectPanel && <ProductCategorySelectPanel
                        brandId={data.brandId}
                        dealerId={data.dealerId}
                        selectKey={details.id}
                        onCancel={this.onCloseProductCategorySelectPanel}
                        onSelect={this.onSelectProductCategory} />
                }
                <div className="page-toolbar">
                    <Return
                        disabled={this.state.loading}
                        onConfirm={this.onClickOkOnPopconfirm}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

AddPanel.propTypes = {
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    getQuantity: PropTypes.func,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    tableSearch,
    saveQueryDealer,
    getQuantity
} from './actions';
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `(${item.code})${item.name}`,
            value: item.id
        })) : [];
        const n1QuantityYearBefore = initData.n1QuantityYearBefore;
        const n1QuantityMonthBefore = initData.n1QuantityMonthBefore;
        return {
            brands,
            n1QuantityYearBefore,
            n1QuantityMonthBefore
        };
    }
);
const mapStateToProps = state => ({
    initEnumData: getinitEnumData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onSelect: data => dispatch(saveQueryDealer(data)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    getQuantity: condition => dispatch(getQuantity(condition)),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(AddPanel));
